package com.clkj.sunhelp.adapter.rv;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.clkj.sunhelp.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * 选择的照片的recycle的adapter
 * Created by Administrator on 2016/3/14.
 */
public class RecycleViewOfChoosedPicAdapter extends RecyclerView.Adapter<RecycleViewOfChoosedPicAdapter.PicViewHolder> {


    List<PhotoInfo> photoInfoList;
    Context context;

    PicItemClickListener picItemClickListener;

    public PicItemClickListener getPicItemClickListener() {
        return picItemClickListener;
    }

    public void setPicItemClickListener(PicItemClickListener picItemClickListener) {
        this.picItemClickListener = picItemClickListener;
    }

    public RecycleViewOfChoosedPicAdapter(List<PhotoInfo> photoInfoList, Context context) {
        this.photoInfoList = photoInfoList;
        this.context = context;
    }

    @Override
    public PicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recycle_item_pic,parent,false);
        PicViewHolder picViewHolder = new PicViewHolder(view);
        return picViewHolder;
    }

    @Override
    public void onBindViewHolder(PicViewHolder holder, final int position) {
        String localPicPath = photoInfoList.get(position).getPhotoPath();
        int targetWidth = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth()/2;
        Picasso.with(context)
                .load("file:"+localPicPath)
                .placeholder(R.drawable.default_pic_small)
                .error(R.drawable.default_pic_small)
                .tag(context)
                .resize(targetWidth,targetWidth)
                .centerCrop()
                .into(holder.iv);

        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null!=picItemClickListener)
                {
                    picItemClickListener.onPicItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return photoInfoList.size();
    }

    class PicViewHolder extends RecyclerView.ViewHolder{

        ImageView iv;


        public PicViewHolder(View itemView) {
            super(itemView);
            this.iv = (ImageView) itemView.findViewById(R.id.iv);
        }
    }

    //图片点击时间
    public interface PicItemClickListener{
        public void onPicItemClick(int position);
    }
}
